package com.erbao.dp.visitor.asm;


import org.objectweb.asm.ClassWriter;

import static org.objectweb.asm.Opcodes.*;
/**
 * @author CaoBaoYin
 * @since 21/9/26 14:47
 */
public class ClassWriterTest {

    public static void main(String[] args) {
        /*动态生成
        * package pkg;
public interface Comparable extends Mesurable {
int LESS = -1;
int EQUAL = 0;
int GREATER = 1;
int compareTo(Object o);
}*/
        ClassWriter cw = new ClassWriter(0);
        // 指定版本，修饰符，名字，父类，父接口
        cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, "pkg/Comparable", null, "java/lang/Object", null);
        cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "LESS","I", null, -1).visitEnd();
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "EQUAL", "I",
                null, new Integer(0)).visitEnd();
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "GREATER", "I",
                null, new Integer(1)).visitEnd();
        cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "compareTo",
                "(Ljava/lang/Object;)I", null, null).visitEnd();
        cw.visitEnd();
        byte[] b = cw.toByteArray();

        // asm用于 新的语言，生成class文件 即 动态生成字节码

        MyClassLoader myClassLoader = new MyClassLoader();
        Class c = myClassLoader.defineClass("pkg.Comparable", b);
        System.out.println(c.getMethods()[0].getName());
    }
}
